其他
一种检测Android SO的UAF和heap over-flow方法
struct sample_1{
char* name;
};
struct sample_2{
char* name;
struct sample_1* v0;
};
int main(int argc,char** argv){
struct sample_1* smp1=(struct sample_1*)malloc(sizeof(struct sample_1));
smp1->name="smp1";
struct sample_2* smp2=(struct sample_2*)malloc(sizeof(struct sample_2));
smp2->name="smp2";
smp2->v0=smp1;
free(smp2);
printf("%s\n",smp2->name);
printf("%s\n",smp2->v0->name);
return 0;
}
struct sample_1{
char* name;
};
struct sample_2{
char* name;
struct sample_1* v0;
};
void show_stack()
{
int i;
void *buffer[1024];
int n = backtrace(buffer, 1024);
char **symbols = backtrace_symbols(buffer, n);
for (i = 0; i < n; i++) {
printf("%s\n", symbols[i]);
}
}
void signal_handler(int sig) {
if(SIGSEGV==sig)
{
show_stack();
exit(-1);
}
else{
printf("signal with %d\n",sig);
}
}
void my_free(void* addr){
printf("free addr:%p size:%d append_size:%d\n",addr,*(size_t*)((size_t)addr-STORESIZE),STORESIZE);
memset(addr,0xFF,*(size_t*)((size_t)addr-STORESIZE));
free((void*)((size_t)addr-STORESIZE));
}
void* my_malloc(size_t len){
void* addr=malloc(len+STORESIZE);
printf("malloc addr:%p size:%d app_size:%d\n",(void*)((size_t)addr+STORESIZE),len,STORESIZE);
*(size_t*)addr=len;
return (void*)((size_t)addr+STORESIZE);
}
int main(int argc,char** argv){
signal(SIGSEGV, signal_handler);
struct sample_1* smp1=(struct sample_1*)my_malloc(sizeof(struct sample_1));
smp1->name="smp1";
struct sample_2* smp2=(struct sample_2*)my_malloc(sizeof(struct sample_2));
smp2->name="smp2";
smp2->v0=smp1;
my_free(smp2);
printf("%s\n",smp2->name);
printf("%s\n",smp2->v0->name);
return 0;
}
printf("free addr:%p size:%d append_size:%d\n",addr,*(size_t*)((size_t)addr-STORESIZE),2*STORESIZE);
memset(addr,0xFF,*(size_t*)((size_t)addr-STORESIZE));
if(*(size_t*)((size_t)addr-STORESIZE)!=((size_t)addr+*(size_t*)((size_t)addr-STORESIZE)))
{
printf("heap over_flow!\n");
show_stack();
exit(-1);
}
free((void*)((size_t)addr-STORESIZE));
}
void* my_malloc(size_t len){
void* addr=malloc(len+2*STORESIZE);
printf("malloc addr:%p size:%d app_size:%d\n",(void*)((size_t)addr+STORESIZE),len,2*STORESIZE);
*(size_t*)addr=len;
*(size_t*)((size_t)addr+len+STORESIZE)=len;
return (void*)((size_t)addr+STORESIZE);
}
int main(int argc,char** argv){
char src[120]="";
void* dst=my_malloc(100);
memcpy(dst,src,120);
my_free(dst);
return 0;
}
看雪ID:scxc
https://bbs.pediy.com/user-638330.htm
推荐文章++++
* Linux Kernel Exploit 内核漏洞学习(4)-RW Any Memory